package com.hunan.api.catv.service.biz.files;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.FilesStateEnum;
import com.hunan.api.catv.entity.enums.FilesTypeEnum;
import com.hunan.api.catv.entity.po.Files;
import com.hunan.api.catv.entity.vo.FilesVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.tencent_cloud.vod.VodBiz;
import com.hunan.api.catv.service.impl.files.FilesService;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@CacheConfig(cacheNames = "FilesBiz")
@Service
public class FilesBiz extends ResultService {

    @Autowired
    FilesService filesService;

    @Autowired
    FilesBiz filesBiz;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    VodBiz vodBiz;

    public ServiceResult add(Files files) {
        if (files.getType().equals(FilesTypeEnum.video.getCode())) {
            String fileId = files.getFileId();
            if (!ObjectUtils.isEmpty(fileId)) {
                FilesVo filesVo = filesBiz.getByFileId(fileId);
                if (!ObjectUtils.isEmpty(filesVo)) {
                    files.setfId(filesVo.getfId());
                    return filesService.update(files);
                }
                files.setState(FilesStateEnum.process.getCode());
                return filesService.add(files);
            }
        }
        return filesService.add(files);
    }

    public ServiceResult update(Files files) {
        if (ObjectUtils.isEmpty(files.getfId())) {
            return buildFail("", "fId is null");
        }
        return filesService.update(files);
    }

    public FilesVo getByFileId(String fileId) {
        Files files = new Files();
        files.setFileId(fileId);
        List<FilesVo> filesVo = filesService.list(files);
        if (!ObjectUtils.isEmpty(filesVo)) {
            return filesVo.get(0);
        }
        return null;
    }

    public PageInfo<FilesVo> list(int pageNum, int pageSize, Integer type, Integer state, String title, String startTime, String endTime) {
        PageHelper.startPage(pageNum, pageSize);
        Files files = new Files();
        files.setType(type);
        files.setState(state);
        files.setIsActivity(0);
        files.setName(title);
        files.setStartTime(startTime);
        files.setEndTime(endTime);
        List<FilesVo> list = filesService.list(files);
        return new PageInfo<>(list);
    }

    public ServiceResult deleteByFileId(String fileId) {
        Integer count = filesService.deleteByFileId(fileId);
        if (count > 0) {
            return vodBiz.deleteMedia(fileId);
        }
        return buildFail("");
    }

    public List<FilesVo> getFileList(String fileId) {
        Files files = new Files();
        files.setFileId(fileId);
        List<FilesVo> filesList = filesService.list(files);
        return filesList;
    }
}
